package answer.service.Impl;

import answer.mapper.CollectionMapper;
import answer.mapper.LikeMapper;
import answer.pojo.LoveC;
import answer.service.LikeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import commons.pojo.Collection;
import commons.pojo.Love;
import commons.vo.LivegoodsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private CollectionMapper collectionMapper;
    @Override
    public List<Love> selectLike(int id) {
        QueryWrapper<Love> collectionQueryWrapper = new QueryWrapper<>();
        collectionQueryWrapper.eq("user", id);
        List<Love> collections = likeMapper.selectList(collectionQueryWrapper);
//        ArrayList<Item> items = new ArrayList<>();
//        for (Love collection : collections) {
//            items.add(itemSearchIdService.getLike(collection.getId()));
//        }
        return collections;
    }

    @Override
    public LivegoodsResult saveLike(Love like) {
        likeMapper.insert(like);
        return LivegoodsResult.ok("已收藏");
    }

    @Override
    public LivegoodsResult deleteLike(int user, String item,int temp) {
        QueryWrapper<Love> collectionQueryWrapper = new QueryWrapper<>();
        collectionQueryWrapper.eq("user", user);
        collectionQueryWrapper.eq("love", item);
        collectionQueryWrapper.eq("category", temp);
        likeMapper.delete(collectionQueryWrapper);
        return LivegoodsResult.ok("已删除");
    }

    @Override
    public LivegoodsResult isLike(int user, int item,int temp) {
        LoveC loveC = new LoveC(0,0);
        QueryWrapper<Love> collectionQueryWrapper = new QueryWrapper<>();
        collectionQueryWrapper.eq("user", user);
        collectionQueryWrapper.eq("love", item);
        collectionQueryWrapper.eq("category", temp);
        Love l = likeMapper.selectOne(collectionQueryWrapper);
        QueryWrapper<Collection> cl = new QueryWrapper<>();
        cl.eq("user", user);
        cl.eq("collection", item);
        cl.eq("category", temp);
        Collection c = collectionMapper.selectOne(cl);
        if (l != null) loveC.setLove(1);
        if (c != null) loveC.setCollection(1);
        return LivegoodsResult.ok(loveC);
    }
}